#ifndef __MINIUTIL_MULTINET_LENFIRSTMSGREADER_H__
#define __MINIUTIL_MULTINET_LENFIRSTMSGREADER_H__

#include "miniutil/socketmultiplex.h"

namespace miniutil { namespace multinet {

class LenFirstMsgSource
{
public:
    virtual int Recv(char* pbuf, int len) = 0;
};

class LenFirstMsgSource_Soc:public LenFirstMsgSource
{
public:
    LenFirstMsgSource_Soc(miniutil::multinet::SocketMulplexService* pse, SOCKET soc);
    virtual int Recv(char* pbuf, int len);
private:
    miniutil::multinet::SocketMulplexService* mpss;
    SOCKET msoc;
};

class LenFirstMsgSource_Fifo:public LenFirstMsgSource
{
public:
    LenFirstMsgSource_Fifo(miniutil::data::fifobuf *pfifo);
    virtual int Recv(char* pbuf, int len);
private:
    miniutil::data::fifobuf *mpfifo;
};

class LenFirstMsgReader
{
    public:
        LenFirstMsgReader();
        ~LenFirstMsgReader();
        int Read(miniutil::multinet::SocketMulplexService* pse, SOCKET soc); //0, not finished, 1 finished, -1 fail
        int Read(miniutil::data::fifobuf* pfifo);
        int Clear();
        int GetMsgBufLen();
        char* RefMsgBuf();
    private:
        int Read(LenFirstMsgSource* psrc);
        
        int msglen;
        int msghaveread;
        char * pbuf;
        
        char tmpbuf[32];
};

} }	

#endif
